package com.wsj.security.extend;



import org.acegisecurity.AuthenticationException;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.providers.dao.DaoAuthenticationProvider;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MKUDaoAuthenticationProvider extends DaoAuthenticationProvider implements UserKindComparisonAware 
{
   private Log logger=LogFactory.getLog(this.getClass());
   
    private String expectedUserKind;
      private String currentUserKind;

    public String getExpectedUserKind() {
          return expectedUserKind;
    }
    public void setExpectedUserKind(String expectedUserKind) {
          this.expectedUserKind = expectedUserKind;
    }

      public String getCurrentUserKind() {
            return currentUserKind;
      }
      public void setCurrentUserKind(String currentUserKind) {
            this.currentUserKind = currentUserKind;
      }

      /******
       * 设置参数到上下文
       * 继承自DaoAuthenticationProvider并实现UserKindComparisonAware接口，
       * 在additionalAuthenticationChecks方法中判断当前登录用户的用户类型标志与期望的用户类型标志是否一致，
       * 如果一致则执行父类的additionalAuthenticationChecks，完成验证；否则抛出一个BadUserKindException异常，
       * 表明验证失败。
       * BadUserKindException继承自org.acegisecurity.AuthenticationException
       */
    protected void additionalAuthenticationChecks(UserDetails userDetails,  UsernamePasswordAuthenticationToken authentication) throws AuthenticationException 
    {
    	logger.debug("expectedUserKind = '" + expectedUserKind + "', currentUserKind = '" + currentUserKind + "'");
        if (currentUserKind.equals(expectedUserKind))
        {
        	System.out.println("认证相同:"+currentUserKind);
        	//调用父类的验证
        	super.additionalAuthenticationChecks(userDetails, authentication);
        }
        else
        {
        	System.out.println("认证不相同:"+currentUserKind);
        	throw new BadUserKindException("Flag UserKind does not match");
        }
            
    }
}